package org.ucas.faker.web.model;

import java.util.*;

public class KeywordNode {
	public String value;
	public Long id;
	public List<KeywordNode> child = new ArrayList<KeywordNode>();
	
	
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public List<KeywordNode> getChild() {
		if (this.child == null){
			child = new ArrayList<KeywordNode>();
		}
		return child;
	}
	public void setChild(List<KeywordNode> child) {
		this.child = child;
	}
	public KeywordNode() {
		// TODO Auto-generated constructor stub
	}
	public KeywordNode(Long id, String value, List<KeywordNode> child) {
		super();
		this.value = value;
		this.id = id;
		this.child = child;
	}
	
	public static void traverse(KeywordNode kn){
		System.out.println(kn.getValue());
		
		if(kn.getChild()!=null){
			for(KeywordNode k:kn.getChild()){
				System.out.print("p "+kn.getValue()+"  ");
				traverse(k);
			}
		}
	}
	
	public static void GetBranchAndEffect(List<KeywordNode> kns, List<String> branch, List<String> effect){
		Set<Long> visit = new HashSet<>();		
		List<KeywordNode> effectTree = new LinkedList<>();
		
		for(KeywordNode kn:kns){
			Stack<KeywordNode> stack = new Stack<>();
			stack.push(kn);
			visit.add(kn.getId());
			//System.out.println(kn.getValue());
			branch.add(kn.getValue());
			
			while(!stack.empty()){
				//System.out.println("pos0   stack size:"+stack.size());
				KeywordNode top = stack.peek();
				int order = 0;
				
				//System.out.println("Child status :"+top.getChild());
				//System.out.println("pos1   "+order+  " child size:"+top.getChild().size()+"   stack size:"+stack.size());
				if(top.getChild()!=null){
					for(order=0;order<top.getChild().size();order++){
						//System.out.println(visit.contains("   " + top.getChild().get(order).getValue()));
						if(visit.contains(top.getChild().get(order).getId())) {
							//System.out.println("   visited");
							continue;
						}
						else {
							stack.push(top.getChild().get(order));

							//System.out.println(top.getChild().get(order).getValue());
							branch.add(top.getChild().get(order).getValue());
							visit.add(top.getChild().get(order).getId());
							
							if(top.getChild().get(order).getValue().equals("技术功效")||
									top.getChild().get(order).getValue().equals("用途")){
								effectTree.add(top.getChild().get(order));
							}
							break;
						}
					}
					
				}
				//System.out.println("pos2   "+order+  "   "+top.child.size()+"   stack size:"+stack.size()+ " top value:"+top.getValue());
				if(top.getChild()==null||order>=top.getChild().size()) {
					//System.out.println("pop");
					stack.pop();
				}
				
			}
			//break;
			//System.out.println();
		}
		
		
		//获取功效
		visit.clear();
		for(KeywordNode kn:effectTree){
			Stack<KeywordNode> stack = new Stack<>();
			stack.push(kn);
			visit.add(kn.getId());
			//System.out.println(kn.getValue());
			effect.add(kn.getValue());
			
			while(!stack.empty()){
				//System.out.println("pos0   stack size:"+stack.size());
				KeywordNode top = stack.peek();
				int order = 0;
				
				//System.out.println("Child status :"+top.getChild());
				//System.out.println("pos1   "+order+  " child size:"+top.getChild().size()+"   stack size:"+stack.size());
				if(top.getChild()!=null){
					for(order=0;order<top.getChild().size();order++){
						//System.out.println(visit.contains("   " + top.getChild().get(order).getValue()));
						if(visit.contains(top.getChild().get(order).getId())) {
							//System.out.println("   visited");
							continue;
						}
						else {
							stack.push(top.getChild().get(order));

							//System.out.println(top.getChild().get(order).getValue());
							effect.add(top.getChild().get(order).getValue());
							visit.add(top.getChild().get(order).getId());
							break;
						}
					}
					
				}
				//System.out.println("pos2   "+order+  "   "+top.child.size()+"   stack size:"+stack.size()+ " top value:"+top.getValue());
				if(top.getChild()==null||order>=top.getChild().size()) {
					//System.out.println("pop");
					stack.pop();
				}
			}
				
			//break;
			//System.out.println();
		}
		
		branch.removeAll(effect);
		
		effect.remove("技术功效");
		effect.remove("用途");
		/*
		for(String s:branch){
			System.out.println(s);
		}System.out.println();
		for(String s:effect){
			System.out.println(s);
		}*/
	}

}
